package org.devcp.tp.common.websocket.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 该过滤器用于修改响应头信息，当nginx下的ajax访问不同的域名下的工程，会因为跨域访问限制而造成无法访问，因为前端页面在无法使用浏览器websocket的时候，
 * 会用socketjs代替，实际是用ajax的方式访问websocket，这样就容易出现跨域访问不到的问题。
 * @author Administrator
 *
 */
public class WebsocketFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain arg2)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		if(resp instanceof HttpServletResponse){
			HttpServletResponse response = (HttpServletResponse)resp;
			response.setHeader("Access-Control-Allow-Origin", "*");
		}
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
